package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.base.MGposSender;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.device.DeviceManager;
import com.moleq.logic.MoleQConstants;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.widget.DisplayScreen;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PosTender;
import com.moleq.mgpos.activities.R;
import com.moleq.util.MoleQUtil;

public class CheckOutDialog extends MoleQBaseDialog {

	private TextView mTextViewTotal;
	private TextView mTextViewChange;
	private TextView mTextViewTenderTotal;
	private Button mButtonPrint;
	private Button mButtonStart;
	// private Button mButtonExit;
	// private OnItemClickListener mActionLinstener;
	private TransactionManager mPaymentMgr;
	private List<PosTender> mDataItems = new ArrayList<PosTender>();
	private Context mContext;
	private ListView mListView;
	private CheckOutTenderAdapter mAdapter;
	private DeviceManager mDeviceManager;
	private TextView mTextViewLabelTotal;
	private TextView mTextViewLabelTender;
	private TextView mTextViewLabelChange;

	class CheckOutTenderAdapter extends BaseAdapter {

		private Context mContext;

		public final class ListItemView {
			public TextView tvTenderDesc;
			public TextView tvPaymentDesc;
			public TextView tvPaid;
		}

		public CheckOutTenderAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataItems.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ListItemView liv = null;
			if (convertView == null) {
				liv = new ListItemView();

				convertView = LayoutInflater.from(mContext).inflate(R.layout.tender_item, null);
				liv.tvTenderDesc = (TextView) convertView.findViewById(R.id.textViewTenderDesc);
				liv.tvPaymentDesc = (TextView) convertView.findViewById(R.id.textViewTenderPaymentDesc);
				liv.tvPaid = (TextView) convertView.findViewById(R.id.textViewTenderPaid);

				convertView.setTag(liv);
			} else {
				liv = (ListItemView) convertView.getTag();
			}

			liv.tvTenderDesc.setText("Tender " + String.valueOf(position + 1));
			liv.tvPaymentDesc.setText(mDataItems.get(position).paymentType.getText());
			liv.tvPaid.setText(MoleQUtil.fixNumber(mDataItems.get(position).getPt_tender()));

			return convertView;
		}

	}

	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.buttonCheckoutPrint:
				onReturnValue(MoleQConstants.PRINT_RECEIPT, "");
				break;
			case R.id.buttonCheckoutStart:
				mPaymentMgr.Reset();
				mPaymentMgr.announceChanges();
				mDeviceManager.enableAllScanner();
				onCloseWithReturn(MoleQConstants.START_TRANSACTION, "");
				break;
			}

		}
	};

	public CheckOutDialog(Context context) {
		super(context, R.style.dialog, context);
		// super(context);
		constructor(context);

	}

	

	public CheckOutDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}

	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;

		// mDataItems = new ArrayList<Tender>();

		// mListView.setOnItemClickListener(onItemClickListener);
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_checkout);
		this.setCancelable(false);

		mButtonPrint = (Button) findViewById(R.id.buttonCheckoutPrint);
		mButtonPrint.setTag(MoleQConstants.PRINT_RECEIPT);
		mButtonPrint.setOnClickListener(mOnClickListener);

		mButtonStart = (Button) findViewById(R.id.buttonCheckoutStart);
		mButtonStart.setTag(MoleQConstants.START_TRANSACTION);
		mButtonStart.setOnClickListener(mOnClickListener);

		// mButtonExit = (Button)findViewById(R.id.buttonCheckoutExit);
		// mButtonExit.setTag(FunctionOption.BACK_TO_LOGIN);
		// mButtonExit.setOnClickListener(mOnClickListener);

		mTextViewTotal = (TextView) findViewById(R.id.textViewCheckoutAmount);
		mTextViewChange = (TextView) findViewById(R.id.textViewCheckoutChange);
		mTextViewTenderTotal = (TextView) findViewById(R.id.textViewTenderTotal);

		mTextViewLabelTotal = (TextView) findViewById(R.id.tvLabelTotal);
		mTextViewLabelTotal.setText(String.format(this.mContext.getResources().getString(R.string.label_total), GlobalInfo.getInstance().CurrencySymbol));

		mTextViewLabelTender = (TextView) findViewById(R.id.tvLabelTender);
		mTextViewLabelTender.setText(String.format(this.mContext.getResources().getString(R.string.label_tender), GlobalInfo.getInstance().CurrencySymbol));

		mTextViewLabelChange = (TextView) findViewById(R.id.tvLabelChange);
		mTextViewLabelChange.setText(String.format(this.mContext.getResources().getString(R.string.label_Change), GlobalInfo.getInstance().CurrencySymbol));

		mListView = (ListView) findViewById(R.id.listViewCheckoutTenderList);

		mAdapter = new CheckOutTenderAdapter(mContext);
		mListView.setAdapter(mAdapter);

	}

	// public void setActionListener (OnItemClickListener linstener){
	// mActionLinstener =linstener;
	// }

	public void show() {
		super.show();

		mDeviceManager = DeviceManager.getInstance();
		mPaymentMgr = TransactionManager.getInstance();

		// mDataItems = mPaymentMgr.getTenderList();
		mDataItems = mPaymentMgr.tenderList;
		mAdapter.notifyDataSetChanged();

		mTextViewTotal.setText(MoleQUtil.fixNumber(mPaymentMgr.getAmountPayable()));
		mTextViewChange.setText(MoleQUtil.fixNumber(mPaymentMgr.getChange()));
		mTextViewTenderTotal.setText(MoleQUtil.fixNumber(mPaymentMgr.getAmountOfPaidUp()));

		DisplayScreen.getInstance().showTenderToDisplayScreen(mPaymentMgr);
		MGposSender.getInstance().send("CHANGE "+GlobalInfo.getInstance().CurrencySign +MoleQUtil.fixNumber(mPaymentMgr.getChange()));
		MGposSender.getInstance().sendRed(MoleQUtil.getSenderString("COMPLETE"));
	}

}
